package com.gs.spring.core.aop;

import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AnnotationPropertyChangeTracker {
	
	private Logger logger = Logger.getLogger(getClass().getName());
	
	@Before("execution(void set*(*))")
	public void trackChange(JoinPoint jp){
		logger.info("Property about to change");
		
		String name = jp.getSignature().getName();
		System.out.println("name :  " + name);
		Object newValue = jp.getArgs()[0];
		System.out.println("About to change to " + newValue + " on " + jp.getThis());
		
	}
	@After("execution(void set*(*))")
	public void trackChanged(){
		logger.info("Property changed");
	}
	
	@AfterReturning("execution(void set*(*))")
	public void trackChangeReturned(){
		logger.info("Control returned");
	}
	
	@AfterThrowing(pointcut="execution(void set*(*))", throwing="error")
	public void catchException(RuntimeException error){
		logger.info("Error da!");
	}
	
	@AfterThrowing(pointcut="execution(void set*(*))", throwing="error")
	public void catchException(ExecutionFailedException error){
		logger.info("ExecutionFailedException da!");
	}
	
}
